<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "org.json.simple.*, com.classes.Casemast, com.classes.CaseOutlet, java.util.ArrayList, java.util.TreeMap" %>
<%@ page import = "com.mbbmap.util.Constants" %>
<%@ page import = "com.mbbmap.security.dao.*" %>
<%@page import="com.mbbmap.util.EncryptionHelper"%>
<%@ include file="/jsp/map/include/user-dao.jsp" %>	
<%
long merchant_id = 0;
long case_id = 0;
long outlet_id = 0;
String vendor_name = "";
String registered_name = "";
String trading_name = "";
String address = "";
String line_1 = "";
String line_2 = "";
String line_3 = "";
String line_4 = "";
String postal_code = "";
String settlement_acc = "";
String settlement_bank = "";
String contact_person = "";

String visa_2 = "000000000000";
String visa_4 = "";
String visa_5 = "";
String union_pay_1 = "";

JSONObject mdr_obj = new JSONObject();
JSONObject vendor_arr = new JSONObject();
ArrayList<TreeMap<String, String>> tid_facilities = new ArrayList<TreeMap<String, String>>();

JSONObject appendix = new JSONObject();

if(request.getAttribute("m_case") != null){
	Casemast m_case = (Casemast) request.getAttribute("m_case");
	
	merchant_id = m_case.getMerchant().getId();
	case_id = m_case.getId();
	registered_name = m_case.getMerchant().getRegisteredName();
}

if(request.getAttribute("c_outlet") != null) {
	CaseOutlet c_outlet = (CaseOutlet) request.getAttribute("c_outlet");
	
	outlet_id = c_outlet.getOutlet().getId();
	trading_name = c_outlet.getOutlet().getName();
	contact_person = c_outlet.getOutlet().getCTName();
	
	if(c_outlet.getOutlet().getAddress().length() > 0){
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getAddress());
	
		line_1 = (obj.get("line_1") == null) ? "" : obj.get("line_1").toString().trim();
		line_2 = (obj.get("line_2") == null) ? "" : obj.get("line_2").toString().trim();
		line_3 = (obj.get("line_3") == null) ? "" : obj.get("line_3").toString().trim();
		line_4 = (obj.get("line_4") == null) ? "" : obj.get("line_4").toString().trim();
		postal_code = (obj.get("postal_code") == null) ? "" : obj.get("postal_code").toString().trim();
		
		String sep = "";
		if(line_1.equals("") == false){
			address += sep + line_1;
			sep = ", ";
		}
		
		if(line_2.equals("") == false){
			address += sep + line_2;
			sep = ", ";
		}
		
		if(line_3.equals("") == false){
			address += sep + line_3;
			sep = ", ";
		}
		
		if(line_4.equals("") == false){
			address += sep + line_4;
			sep = ", ";
		}
	}
	
	
	if(c_outlet.getOutlet().getPaymentOpt().length() > 0) {
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getPaymentOpt());
		
		visa_2 = (obj.get("visa_2") == null) ? "" : obj.get("visa_2").toString();
		visa_4 = (obj.get("visa_4") == null) ? "" : obj.get("visa_4").toString();
		visa_5 = (obj.get("visa_5") == null) ? "" : obj.get("visa_5").toString();
		
		union_pay_1 = (obj.get("union_pay_1") == null) ? "" : obj.get("union_pay_1").toString();
		
		if(union_pay_1.equals("credit_maybank")){
			settlement_acc = visa_2;
			settlement_bank = "MAYBANK";
		}
		else if(union_pay_1.equals("interbank_giro")){
			settlement_acc = visa_5;
			settlement_bank = visa_4;
			
			if(visa_4.equals("002")){
				settlement_bank = "AFFIN BANK BERHAD";
			} else if(visa_4.equals("003")){
				settlement_bank = "ALLIANCE BANK M'SIA BHD";
			} else if(visa_4.equals("004")){
				settlement_bank = "AMBANK BERHAD";
			} else if(visa_4.equals("005")){
				settlement_bank = "BANK ISLAM MALAYSIA BHD";
			} else if(visa_4.equals("006")){
				settlement_bank = "CIMB BANK BERHAD";
			} else if(visa_4.equals("007")){
				settlement_bank = "EON BANK BERHAD";
			} else if(visa_4.equals("008")){
				settlement_bank = "HONG LEONG BANK BHD";
			} else if(visa_4.equals("009")){
				settlement_bank = "PUBLIC BANK BHD";
			} else if(visa_4.equals("010")){
				settlement_bank = "RHB BANK BERHAD";
			} else if(visa_4.equals("012")){
				settlement_bank = "BANK MUAMALAT (M'SIA)";
			} else if(visa_4.equals("013")){
				settlement_bank = "CITIBANK BERHAD";
			} else if(visa_4.equals("014")){
				settlement_bank = "OCBC BANK(M'SIA) BHD";
			} else if(visa_4.equals("015")){
				settlement_bank = "HSBC BERHAD";
			} else if(visa_4.equals("016")){
				settlement_bank = "BANK RAKYAT MALAYSIA";
			} else if(visa_4.equals("017")){
				settlement_bank = "STANDARD CHARTERED BANK";
			} else if(visa_4.equals("018")){
				settlement_bank = "BANK SIMPANAN M'SIA";
			} else if(visa_4.equals("019")){
				settlement_bank = "UNITED OVERSEAS BANK";
			} else if(visa_4.equals("020")){
				settlement_bank = "DEUTSCHE BANK(M) BHD";
			}
		}
	}
	
	
	if(c_outlet.getOutlet().getMDRRate().length() > 0){
		mdr_obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getMDRRate());
	}
	
	if(c_outlet.getPosForm().length() > 0){
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getPosForm());
		
		if(obj.get("vendor_arr") != null){
			vendor_arr = (JSONObject) obj.get("vendor_arr");
		}
	}
}


if(request.getParameter("vendor_name") != null) {
	vendor_name = EncryptionHelper.decrypt(request.getParameter("vendor_name"));
	JSONObject vendor_details = (JSONObject) vendor_arr.get(vendor_name);
	JSONObject facility_tids = (vendor_details.get("facility_tids") == null ? null : (JSONObject) vendor_details.get("facility_tids"));
	
	String[] arr_type = new String[] {"Wired", "Wireless", "MPOS"};
	
	for(Object s : facility_tids.keySet()){
		JSONObject tid_type_obj = (JSONObject) facility_tids.get(s);
		
		for(String t  : arr_type) {
			if(tid_type_obj.get(t) != null) {
				JSONArray tid_obj = (JSONArray) tid_type_obj.get(t);
				
				for(int i=0; i<tid_obj.size(); i++){
					JSONObject tid_item = (JSONObject) tid_obj.get(i);
					
					TreeMap<String, String> item = new TreeMap<String, String>();
					item.put("vendor", vendor_details.get("name").toString());
					item.put("facility", s.toString());
					item.put("mid_no", tid_item.get("mid_no").toString());
					item.put("tid_no", tid_item.get("tid_no").toString());
					
					String in_mcc = mdr_obj.get("22").toString();	// VISA MASTER POS MCC Code
					String in_unionpay = mdr_obj.get("110").toString();	// VISA MASTER POS UnionPay
					
					
					item.put("mcc_code", in_mcc);
					item.put("mdr", in_unionpay);
					
					tid_facilities.add(item);
				}
			}
		}
	}
}

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/jsp/map/include/html-meta.jsp" />
<script src="<%= request.getContextPath() %>/js/vendor.js"></script>
<title>MAYBANK</title>
</head>
<body>

<!-- Header -->
<%@ include file="/jsp/common/_Header_Email.jsp" %>	
<!-- End Header -->

<div class="row-fluid page">
	<div class="span12">
		
		<table class="tbl vendor_pos_tbl_1" style="width:100%; margin-bottom:20px"><thead>
			<tr>
				<th style="width:6%">No.</th>
				<th style="width:6%">Terminal ID</th>
				<th style="width:6%">CUP TID</th>
				<th style="width:6%">Vendor</th>
				<th style="width:6%">Terminal Brand</th>
				<th style="width:6%">Merchant Code (MBB MID)</th>
				<th style="width:6%">CUP MID</th>
				<th style="width:6%">Merchant Name</th>
				<th style="width:6%">Legal Name of Company</th>
				<th style="width:6%">MCC</th>
				<th style="width:6%">MDR</th>
				<th style="width:6%">Settlement Account Name</th>
				<th style="width:6%">Settlement Account</th>
				<th style="width:6%">Settlement Bank</th>
				<th style="width:6%">Contact Person</th>
				<th style="width:6%">Merchant Address</th>
				<th style="width:6%">Postcode</th>
			</tr>
		</thead><tbody>
		<%
			int i = 0;
			for(TreeMap<String, String> item : tid_facilities){
				++i;
				
				String html = "<tr>"
					+ "	<td>" + i + "</td>"
					+ "	<td>" + item.get("tid_no") + "</td>"
					+ "	<td>" + item.get("tid_no") + "</td>"
					+ "	<td>" + item.get("vendor") + "</td>"
					+ "	<td></td>"
					+ "	<td>" + item.get("mid_no") + "</td>"
					+ "	<td></td>"
					+ "	<td>" + trading_name + "</td>"
					+ "	<td>" + registered_name + "</td>"
					+ "	<td>" + item.get("mcc_code") + "</td>"
					+ "	<td>" + item.get("mdr") + "</td>"
					+ "	<td>" + registered_name + "</td>"
					+ "	<td>" + settlement_acc + "</td>"
					+ "	<td>" + settlement_bank + "</td>"
					+ "	<td>" + contact_person + "</td>"
					+ "	<td>" + address + "</td>"
					+ "	<td>" + postal_code + "</td>"
					+ "</tr>";
				
				out.println(html);
			}
		%>
		</tbody></table>
		
		
		<form name="form" class="form-horizontal" style="margin-top:40px">
			<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
			<div class="control-group">
				<input name="email" type="text" placeholder="Email address" value="<%= request.getAttribute("email_addr").toString() %>" />
				<button id="notify" type="button" class="btn" onclick="vendor.create_appendix_unionpay(<%= merchant_id %>, <%= case_id %>, <%= outlet_id %>, '<%=vendor_name %>', true)">Notify via Email</button>
			</div>
		</form>
		
	</div>
</div>

</body>
</html>